﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

#region Material
namespace backoffice.Data
{
    partial class Material
    {
        public void setMaterial(Material m)
        {
            this.MaterialID = m.MaterialID;
            this.Name = m.Name;
            this.Description = m.Description;
            this.Image = m.Image;
        }
    }
}
#endregion

#region dataMaterial
namespace backoffice.Data
{
    public class dataMaterial
    {
        #region Create
        public static void createMaterial(Material m)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                m.MaterialID = Guid.NewGuid();

                db.Materials.InsertOnSubmit(m);
                db.SubmitChanges();
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Get
        public static Material getMaterial(Guid guid)
        {
            try
            {
                if (guid == null) return null;

                db2growDataContext db = new db2growDataContext();

                Material res = (from m in db.Materials
                                where m.MaterialID == guid
                                select m).FirstOrDefault();

                return res;
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        public static IQueryable<Material> getMaterials(Expression<Func<Material, bool>> whereClause)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                return db.Materials.Where(whereClause);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Set
        public static Boolean changeMaterial(Material m)
        {
            try
            {
                if (m == null)
                {
                    return false;
                }
                else
                {
                    db2growDataContext db = new db2growDataContext();

                    Material res = db.Materials.FirstOrDefault(material => material.MaterialID == m.MaterialID);

                    if (res == null)
                    {
                        return false;
                    }
                    else
                    {
                        res.setMaterial(m);

                        db.SubmitChanges();

                        return true;
                    }
                }

            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion

        #region Delete
        public static Boolean deleteMaterial(Material m)
        {
            try
            {
                db2growDataContext db = new db2growDataContext();

                if (db.Materials.Contains(m))
                {
                    Material res = db.Materials.FirstOrDefault(material => material.MaterialID == m.MaterialID);

                    if (res == null)
                    {
                        return false;
                    }
                    else
                    {
                        // Delete sells
                        IQueryable<Sell> sells = db.Sells.Where((Sell s) => (s.MaterialID == res.MaterialID));
                        db.Sells.DeleteAllOnSubmit(sells);

                        // Delete method_material
                        IQueryable<Methods_Material> mm = db.Methods_Materials.Where((Methods_Material m_m) => m_m.MaterialID == res.MaterialID);
                        db.Methods_Materials.DeleteAllOnSubmit(mm);

                        // Delete material
                        db.Materials.DeleteOnSubmit(res);
                        db.SubmitChanges();

                        return true;
                    }
                }
                else
                {
                    return false;
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        #endregion
    }
}
#endregion